<?php	
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
require_once($GLOBALS['PAD']."Aplications/ReportModels/report.vars.php");
MYTCONN();
	include($GLOBALS['OBJ'].'obj.mathgrids.mex');
	$NoResults = '<tr bgcolor="#F2F2F2"><td colspan="13">NÃo existem resultados para apresentação.</td></tr>';

	$FinantialResult = 0;
	if(empty($_POST)){
		$_POST['Tini'] = date("Y-m-01");
		$_POST['Tend'] = date("Y-m-d");
		$_POST['Origens'] = 'OrderOffice';
		$_POST['Countries'] = '301,305,300';
	}
	
	$Tini = $_POST['Tini'];
	$Tend = $_POST['Tend'];

	$NClCountry = " AND BnfPais = 76";
	$QrCountry = " AND FraCorresponsal IN ({$_POST['Countries']})";
	$ClCountry = " AND BnfPais = 76";
	$currency = '';
	echo $tablehead;
	echo '<tr align="center">
	<td colspan="13" bgcolor="#FFFFFF">Resultado de Promocoes activas no periodo entre '.$Tini.' e '.$Tend.'</td></tr>
	<tr align="center" bgcolor="#D7E3E8">
	<td width="17%">Promocao</td>
	<td width="7%">Valor Atribuido</td>
	<td width="9%">Atribuidos</td>
	<td width="9%">Total Atribuido</td>
	<td width="10%">Total Enviado</td>
	<td width="8%">Media Enviado</td>
	<td width="8%">Clientes Unicos</td>
	<td width="10%">Total Clientes</td>
	</tr>';
	
	$GET = $GLOBALS['MYT']->db_array("SELECT prom_Description, Prom_promValue, COUNT(FraProm) AS CtProm,SUM(FraProm) AS SumProm, SUM(FraEnviado) AS SumEnviado,  AVG(FraEnviado) AS AvgEnviado, COUNT(DISTINCT FraCliNumero) AS CtUniqClient, COUNT(FraID) AS CtClient
FROM promotions_entries
  INNER JOIN facturas ON pme_Fra = FraID
  INNER JOIN promotions ON pme_Prom = REF
  INNER JOIN origenes ON OriNumero = FraOrigen0
  WHERE pme_Time BETWEEN '{$Tini}' AND '{$Tend}'
GROUP BY pme_Prom"); 
	
	$CGET = $GET[0]+1;
if (!isset($GET[1])) {
	echo $NoResults; 
} else {
	for($h = 1; $h <= $CGET; $h++) {

    echo '<tr bgcolor="'.gridlines($h).'"</td>
	<td class="BoxField">'.FieldResult($GET,$h,'prom_Description').'</td>
	<td align="right" class="BoxField">'.number_format(FieldResult($GET,$h,'Prom_promValue'),2,'.','').$currency.'</td>
	<td align="right" class="BoxField">'.FieldResult($GET,$h,'CtProm').'</td>
	<td align="right" class="BoxField">'.number_format(FieldResult($GET,$h,'SumProm'),2,'.','').$currency.'</td>
	<td align="right" class="BoxField">'.number_format(FieldResult($GET,$h,'SumEnviado'),2,',','').$currency.'</td>
	<td align="right" class="BoxField">'.number_format(FieldResult($GET,$h,'AvgEnviado'),2,'.','').$currency.'</td>
	<td align="right" class="BoxField">'.FieldResult($GET,$h,'CtUniqClient').$currency.'</td>
	<td align="right" class="BoxField">'.FieldResult($GET,$h,'CtClient').$currency.'</td>
	</tr>';
	}
} ?>